import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { ApiCode } from "@/mdesign-lib/mdesign-app.umd.js";
import { localFile } from "@/mdesign-lib/mdesign-app.umd.js";
import { modelTreeMultiSelectWindow } from "@/mdesign-lib/mdesign-app.umd.js";
import { archService } from "@/mdesign-lib/mdesign-app.umd.js";
import { PropertyDataType } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";

export class EWXExportAction extends Action {
  keyName = "EWXExport" as ActionKey;
  desc = "EWXExport";

  async run() {
    const projectId = app.activeProject?.id;
    if (!projectId) throw new Error("无激活项目！");
    await modelTreeMultiSelectWindow.popup(
      {
        loadMethod: "getPropertyTreeList",
        showNodeLabel: true,
        loadParams: JSON.stringify({
          row: {
            dataType: PropertyDataType.INSTANCE,
            moreBtnCondition: "16902438067776",
            key: "",
            modelId: "16902438067776",
            checkMethod: null,
            nullable: false
          },
          projectId
        }),
        multiple: true,
        checkable: false,
        delay: true,
        attrTip: "radio",
        title: "选择模型"
      },
      async res => {
        if (!res.success) return;
        const selectedId = res.selectedId as string[];
        const chooseFilePathResult = await localFile.chooseSaveFilePath({
          title: "选择导出位置",
          defaultPath: "Untitled.xml",
          filters: [
            {
              name: "xml",
              extensions: ["xml"]
            }
          ]
        });
        const filePath = chooseFilePathResult.filePath;
        if (chooseFilePathResult.canceled || !filePath) {
          modelTreeMultiSelectWindow.abort();
          return;
        }
        const result = await archService.exportEWXXML(projectId, selectedId, filePath);
        if (result.code === ApiCode.SUCCESS) {
          app.$notify({
            type: "success",
            title: "成功",
            message: "导出成功"
          });
        }
        modelTreeMultiSelectWindow.done();
      }
    );
  }

  onSysMenuClick() {
    this.run();
  }
}
